package com.hziee.mail.service;

import com.hziee.mail.dao.GoodDetailRepository;
import com.hziee.mail.dao.OrderInfoRepository;
import com.hziee.mail.dao.OrderTemporaryRepository;
import com.hziee.mail.dao.SkuInfoRepository;
import com.hziee.mail.model.OrderInfoEntity;
import com.hziee.mail.model.OrderInfoTemporaryEntity;
import com.hziee.mail.model.SkuInfoEntity;
import com.hziee.mail.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author ZX
 */
@Service
public class OrderService {

    @Autowired
    private OrderTemporaryRepository orderTemporaryRepository;

    @Autowired
    private OrderInfoRepository orderInfoRepository;

    @Autowired
    private SkuInfoRepository skuInfoRepository;

    @Autowired
    private GoodDetailRepository goodDetailRepository;

    public String addOrderTemporary(String goodsId, Integer value, double price, Integer userId) {
        orderTemporaryRepository.insertOrderTemporary(goodsId, value, price, userId);
        return "success";
    }

    public List<List<Map>> getOrderTemporary(String[] goodsId, Integer userId) {
        List<List<Map>> list = new ArrayList<>();
        for (int i = 0; i < goodsId.length; i++) {
            list.add(orderTemporaryRepository.getOrderTemporary(goodsId[i], userId));
        }
        return list;
    }

    public String addOrderInfo(OrderInfoEntity orderInfoEntity) {
        orderInfoEntity.setOrderTime(TimeUtil.outTime());
        orderInfoRepository.save(orderInfoEntity);
        return "success";
    }

    public List<Map> getOrderInfo(Integer userId, Integer type) {
        List<Map> list = new ArrayList<Map>();
        if (type == 0) {
            list = orderInfoRepository.getOrderInfoType(userId, type);
        } else if (type == 1) {
            list = orderInfoRepository.getOrderInfoType(userId, type);
        } else if (type == 2) {
            list = orderInfoRepository.getOrderInfoType(userId, type);
        } else if (type == 3) {
            list = orderInfoRepository.getOrderInfoType(userId, type);
        } else if (type == 4) {
            list = orderInfoRepository.getOrderInfo(userId);
        }
        return list;
    }


    public List<Map> getOrderDetails(String order_number) {
        return orderInfoRepository.getOrderDetails(order_number);
    }

    public String reduceAmount(Integer value, String goodsId) {
        skuInfoRepository.reduceAmount(value, goodsId);
        return "success";
    }

    public Integer findAmount(String goodsId) {
        return skuInfoRepository.findAllByProductClassId(goodsId).getAmount();
    }
}
